package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.CollectionUtil;
import com.ccp.dev.qbdms.dao.SysOrgDeployDao;
import com.ccp.dev.qbdms.model.SysOrgDeploy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 系统部署方式管理, 信息操作服务类
 *
 * @author zzy
 * @date 2020-08-03 15:43:36
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysOrgDeployService extends BaseService<SysOrgDeploy> {

    @Resource
    private SysOrgDeployDao dao;

    private static final String YES = "1";
    private static final String NO = "0";


    /**
     * 根据组织code获取对应的部署方式
     * @param orgcode
     * @return
     */
    public SysOrgDeploy getDeployMethodByCode(String orgcode){
        Map<String,Object> map = new HashMap<>(3);
        map.put("isParent", NO);
        map.put("orgCode", orgcode);
        map.put("tenantId", ContextUtil.getCurrentUserTenantId());
        List<SysOrgDeploy> datalist = dao.queryList(map);
        if (CollectionUtil.isEmpty(datalist)) {
            return null;
        }
        return datalist.get(0);
    }


    /**
     * 获取当前所属租户下的全部下级单位的部署方式
     *  使用如下值判断部署方式
     *  ZhiLiangConst.DEPMETHOND_CLASS
     *  ZhiLiangConst.DEPMETHOND_CENTER
     * @return 返回一个部署方式对象,只需要遍历列表,判断部署方式即可
     */
    public List<SysOrgDeploy> getAllSonDeployMethod(){
        return dao.getAllSonDeply(ContextUtil.getCurrentOrgId());
    }

    /**
     * 根据组织code获取对应的部署url
     * @param orgcode
     * @return 查不到则返回空串
     */
    public String getOrgUrlByCode(String orgcode){
        SysOrgDeploy temp = getDeployMethodByCode(orgcode);
        if (BeanUtils.isEmpty(temp)) {
            return "";
        }
        return temp.getProtocol()+"://"+temp.getIp() +":"+ temp.getPort();
    }

    /**
     * 获取当前所属租户配置过的上级单位部署方式
     * @return
     */
    public SysOrgDeploy getDeployMethod(){
        Map<String,Object> map = new HashMap<>();
        // 获取当前租户下的上级单位信息,如果没有则默认按照集中部署处理
        map.put("isParent", YES);
        map.put("tenantId", ContextUtil.getCurrentUserTenantId());
        List<SysOrgDeploy> datalist = dao.queryList(map);
        if (CollectionUtil.isEmpty(datalist)) {
            return null;
        }
        return datalist.get(0);
    }



}
